#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

// 共享内存写程序
int main (int argc, char **argv)
{
	int shm_id;
	int *ptr = NULL;

	shm_id = shmget(12345, 4, 0666 | IPC_CREAT);
	if (shm_id == -1 || shm_id == 0)
	{
		perror("shm_id");
		exit(-1);
	}

	if (NULL == (ptr = shmat(shm_id, NULL, 0)))
	{
		perror("shmat");
		return -1;
	}

	*ptr = 1;
	while(1)
	{
		sleep(1);
		(*ptr)++;
	}

	return 0;
}
